package com.flow.demo.module.gd.controller.admin.user;

import com.flow.demo.module.gd.dal.dataobject.gduser.GdUserDO;
import com.flow.demo.module.gd.service.gduser.GdUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.io.IOException;
import java.util.List;

import com.flow.demo.framework.common.pojo.PageParam;
import com.flow.demo.framework.common.pojo.PageResult;
import com.flow.demo.framework.common.pojo.CommonResult;
import com.flow.demo.framework.common.util.object.BeanUtils;
import static com.flow.demo.framework.common.pojo.CommonResult.success;

import com.flow.demo.framework.excel.core.util.ExcelUtils;

import com.flow.demo.framework.operatelog.core.annotations.OperateLog;
import static com.flow.demo.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.flow.demo.module.gd.controller.admin.user.vo.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 客户信息")
@RestController
@RequestMapping("/gd/user")
@Validated
public class GdUserController {

    @Resource
    private GdUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建客户信息")
    @PreAuthorize("@ss.hasPermission('gd:user:create')")
    public CommonResult<Integer> createUser(@Valid @RequestBody GdUserSaveReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户信息")
    @PreAuthorize("@ss.hasPermission('gd:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody GdUserSaveReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('gd:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Integer id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('gd:user:query')")
    public CommonResult<GdUserRespVO> getUser(@RequestParam("id") Integer id) {
        GdUserDO user = userService.getUser(id);
        return success(BeanUtils.toBean(user, GdUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户信息分页")
    @PreAuthorize("@ss.hasPermission('gd:user:query')")
    public CommonResult<PageResult<GdUserRespVO>> getUserPage(@Valid GdUserPageReqVO pageReqVO) {
        PageResult<GdUserDO> pageResult = userService.getUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GdUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户信息 Excel")
    @PreAuthorize("@ss.hasPermission('gd:user:export')")
    @OperateLog(type = EXPORT)
    public void exportUserExcel(@Valid GdUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GdUserDO> list = userService.getUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户信息.xls", "数据", GdUserRespVO.class,
                        BeanUtils.toBean(list, GdUserRespVO.class));
    }

}